package com.shizhanzhe.zxbapp.task.newshare;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.customtask.PublishActivity;
import com.shizhanzhe.zxbapp.pay.PayVIP;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.zxbapp.widget.VipDIalog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by YJ on 2019/1/3.
 */
public class ForWardItemFragment extends Fragment {
    ListView lvShare;
    ImageView nodata;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    Unbinder unbinder;
    private ShareAdapter adapter;
    public static ForWardItemFragment newInstance(String id) {
        Bundle args = new Bundle();
        args.putString("id", id);
        ForWardItemFragment fragment = new ForWardItemFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_item_forward, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        nodata=view.findViewById(R.id.nodata);
        lvShare=view.findViewById(R.id.lv_share);
        refresh.setRefreshHeader(new ClassicsHeader(getContext()));
        refresh.setRefreshFooter(new ClassicsFooter(getContext()));
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                list.clear();
                getData();
                refreshLayout.finishRefresh(2000);
            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.NEWSHARE(getArguments().getString("id"), page, UserBean.uid), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

                    @Override
                    public void onsendJson(String json) {
                        if (!"1".equals(json) && !"2".equals(json)) {
                            Gson gson = new Gson();
                            ShareList shareList = gson.fromJson(json, ShareList.class);
                            List<ShareList.DataBean> lists = shareList.getData();
                            if (lists.size()==0) {
                                Toast.makeText(getContext(), "没有更多了", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            list.addAll(lists);
                            adapter.notifyDataSetChanged();
                        } else {
                            Toast.makeText(getActivity(), "网络异常", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                refreshLayout.finishLoadMore(2000);
            }
        });


        getData();
        lvShare.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (UserBean.level > 0) {
                    Intent intent = new Intent(getActivity(), ShareWebActivity.class).putExtra("id", list.get(i).getArtid());
                    startActivity(intent);
                } else {
                    if(list.get(i).getIsvip()==1){
                        final VipDIalog vipDIalog = new VipDIalog(getContext());
                        vipDIalog.setVIPListener(new VipDIalog.VIPListener() {
                            @Override
                            public void chooseTrue() {
                                startActivity(new Intent(getContext(), PayVIP.class));
                                vipDIalog.dismiss();
                            }

                            @Override
                            public void chooseFalse() {
                                vipDIalog.dismiss();
                            }
                        });
                        vipDIalog.show();
                    }else {
                        Intent intent = new Intent(getActivity(), ShareWebActivity.class).putExtra("id", list.get(i).getArtid());
                        startActivity(intent);
                    }

                }
            }
        });
    }

    int page = 1;
    List<ShareList.DataBean> list = new ArrayList<>();

    private void getData() {

        //type 0 可分享 1 已分享
        OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.NEWSHARE(getArguments().getString("id"), page,UserBean.uid), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    try {
                        Gson gson = new Gson();
                        ShareList shareList = gson.fromJson(json, ShareList.class);
                        list = shareList.getData();
                        if (list.size() == 0) {
                            lvShare.setEmptyView(nodata);
                        } else {
                            adapter = new ShareAdapter(list, getContext(),"","");
                            lvShare.setAdapter(adapter);
                        }
                    }catch (Exception e){

                    }
                } else {

                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
